<template>
  <div class="areasManagement-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.name" clearable placeholder="请输入省份" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" @selection-change="setSelectRows">
      <el-table-column show-overflow-tooltip type="selection" />
      <el-table-column label="id" prop="id" show-overflow-tooltip />
      <el-table-column label="地区编码" prop="areaid" show-overflow-tooltip />
      <el-table-column label="地区名称" prop="area" show-overflow-tooltip />
      <el-table-column label="城市编码" prop="cityid" show-overflow-tooltip />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<style scoped>
  .tag-container {
    display: flex;
    flex-wrap: wrap;
    min-width: 100px;
  }

  .tag {
    margin-right: 8px;
    margin-bottom: 8px;
    white-space: normal; /* 允许内容换行 */
    display: inline-block; /* 确保每个 tag 在同一行内 */
  }

  .status-enable,
  .status-disable {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
  }

  .status-enable {
    border: 1px solid #6abf69; /* 绿色边框 */
    color: #6abf69; /* 绿色文本 */
  }

  .status-disable {
    border: 1px solid #f56c6c; /* 红色边框 */
    color: #f56c6c; /* 红色文本 */
  }
</style>

<script>
  import {getAllAreas} from '@/api/address'

  export default {
    name: 'AreasManagement',
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          username: '',
        },
        timeOutID: null,
      }
    },
    created() {
      this.fetchData()
    },

    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getAllAreas(this.queryForm)
        this.list = data.list
        this.total = data.totalCount
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
